<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.alibaba.fastjson.JSONObject"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.net.URL"%>
<%@page import="java.net.URLConnection"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="com.weixinpay.*"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="com.orangelife.utils.HttpSvcUtils"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
	
	//获取code
	String code = request.getParameter("code");
	//System.out.println(code);
	String result = "";
	BufferedReader in = null;
	
	//通过code获取openid;
	try {
		 String  httpUrl = "https"+"://"+"api.weixin.qq.com/sns/oauth2/access_token?appid=wx84ef6813a5126fc8&secret=eed244f44ae45cb8d0ccb778bba59a23&code="+code+"&grant_type=authorization_code";
		 //System.out.println(httpUrl);
         URL realUrl = new URL(httpUrl);
         // 打开和URL之间的连接
         URLConnection connection = realUrl.openConnection();
         // 设置通用的请求属性
         connection.setRequestProperty("accept", "*/*");
         connection.setRequestProperty("connection", "Keep-Alive");
         connection.setRequestProperty("user-agent",
                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
         // 建立实际的连接
         connection.connect();
         // 获取所有响应头字段
         Map<String, List<String>> map = connection.getHeaderFields();
         // 定义 BufferedReader输入流来读取URL的响应
         in = new BufferedReader(new InputStreamReader(
                 connection.getInputStream()));
         String line;
         while ((line = in.readLine()) != null) {
        	 result += line;
         }
     } catch (Exception e) {
         System.out.println("发送GET请求出现异常！" + e);
         e.printStackTrace();
     }
     // 使用finally块来关闭输入流
     finally {
         try {
             if (in != null) {
                 in.close();
             }
         } catch (Exception e2) {
             e2.printStackTrace();
         }
     }
	//System.out.println(result);
	JSONObject jsonObject = JSONObject.parseObject(result);
	String openId = jsonObject.getString("openid");
	
	//通过openid调用方法获取prepayID以及其他相关属性
	WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
	IWeiXinPayWebService wp = (IWeiXinPayWebService) wac.getBean("WeiXinPayWebService");
	System.out.println(openId);
	Map<String,String> map = wp.weiXinJsPayJsp(openId, "缴费支付", "缴费支付",new Date().getTime()+"1111", "0.01", "10.168.8.64", "JSAPI");
	String appID = map.get("appID").toString();
	String timeStamp = map.get("timeStamp").toString();
	String nonceStr = map.get("nonceStr").toString();
	String prepayID = map.get("prepayID").toString();
	String signType = map.get("signType").toString();
	String sign = map.get("sign").toString();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>TestWeixin</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="TestWeixin">
<meta http-equiv="description" content="TestWeixin">
<script type="text/javascript">
    
    //页面在微信中加载时onload()调用onBridgeReady()方法，进行支付
	function onBridgeReady() {
		WeixinJSBridge.invoke('getBrandWCPayRequest', {
			"appId" : "<%=appID%>", //公众号名称，由商户传入
			"timeStamp" : "<%=timeStamp%>", //时间戳，自1970年以来的秒数
			"nonceStr" : "<%=nonceStr%>", //随机串
			"package" : "prepay_id=<%=prepayID%>",
			"signType" : "<%=signType%>", //微信签名方式:
			"paySign" : "<%=sign%>" //微信签名

		}, function(res) {
			if (res.err_msg == "get_brand_wcpay_request:ok") {
			}
			//alert(JSON.stringify(res));
		});
	}

	function callpay() {

		if (typeof WeixinJSBridge == "undefined") {
			if (document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
						false);
			} else if (document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
				document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
			}
		} else {
			onBridgeReady();
		}

	}
</script>

</head>

<body onload="callpay()">

</body>
</html>
